// components/memberInfo/memberInfo.js
import api from "../../api/index";
const App = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: () => {
        return {};
      },
    },
    isGo: {
      type: Boolean,
      value: true,
    },
    isExtra: {
      type: Boolean,
      value: false,
    },
    extra: {
      type: Object,
      value: () => {
        return {};
      },
    },
  },
  observers: {
    extra(v) {
      this.setData({
        guide: v.guide_coach_info,
        assign: v.assign_coach_info,
        "remarkData.value": v.remark,
      });
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    colors: ["#47CC88", "#73F0EA"],
    remarkData: {
      title: "编辑备注",
      placeholder: "请填写备注",
      value: "",
    },
    remarkShow: false,

    guide: null,
    assign: null,
    //#region 教练
    coachShow: false,
    coachObj: {
      name: "coach_id",
    },
    //#endregion
  },
  attached() {
    this.getCoachList();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    deleteFace() {
      wx.showModal({
        title: "确认删除",
        content: `确认删除用户人脸？`,
        confirmText: "删除",
        cancelText: "取消",
        confirmColor: "#73F0EA",
        success: (res) => {
          if (res.confirm) {
            api
              .apiStoreDelFace({
                user_id: data.user_id,
              })
              .then((res) => {
                if (res.code === 1) {
                  wx.showToast({
                    title: "删除成功",
                    icon: "none",
                  });
                  this.setData({
                    "extra.is_face": 0,
                  });
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: "none",
                  });
                }
              });
          }
        },
      });
    },
    getCoachList() {
      this.setData({
        "coachObj.list": App.globalData.coachList,
      });
    },
    getCoach(e) {
      let { data, coachObj } = this.data;
      let coachList = coachObj.list;
      api
        .apiStoreAssign({
          user_id: data.user_id,
          coach_id: e.detail.value,
        })
        .then((res) => {
          if (res.code === 1) {
            let item = coachList.find((i) => i.coach_id === e.detail.value);
            this.setData({
              "extra.assign_form": "have",
              assign: item,
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
            });
          }
        });
    },
    openCoach(e) {
      this.setData({
        coachShow: true,
      });
    },
    closeCoach() {
      this.setData({
        coachShow: false,
      });
    },
    openRemark() {
      this.setData({
        remarkShow: true,
      });
    },
    onClose() {
      this.setData({
        remarkShow: false,
      });
    },
    previewImage(e) {
      wx.previewImage({
        urls: e.currentTarget.dataset.url.split(";"),
      });
    },
    onConfirm(e) {
      api
        .apiStoreRemark({
          user_id: this.data.data.user_id,
          remark: e.detail,
        })
        .then((res) => {
          if (res.code === 1) {
            wx.showToast({
              title: "设置成功",
              icon: "none",
            });
            this.setData({
              "remarkData.value": e.detail,
              remarkShow: false,
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
            });
          }
        });
    },
    goDetail() {
      if (!this.data.isGo) return;
      wx.navigateTo({
        url: "/pages/member/detail/index?id=" + this.data.data.user_id,
      });
    },
    copyText(e) {
      wx.setClipboardData({
        data: e.currentTarget.dataset.text,
        success: function (res) {
          wx.showToast({
            title: "复制成功",
            icon: "success",
          });
        },
        fail: function (err) {
          console.error("复制失败:", err);
        },
      });
    },
    // 通用拨打电话方法
    makeCall(e) {
      let phoneNumber = e.currentTarget.dataset.text;
      // 显示确认对话框
      wx.showModal({
        title: "确认拨打电话",
        content: `是否拨打 ${phoneNumber}？`,
        confirmText: "拨打",
        cancelText: "取消",
        confirmColor: "#73F0EA",
        success: (res) => {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: phoneNumber,
              success: () => {
                // 可以在这里记录拨打日志
                this.recordCallLog(phoneNumber);
              },
              fail: (err) => {
                console.error("拨打失败:", err);
                wx.showToast({
                  title: "拨打失败，请重试",
                  icon: "none",
                });
              },
            });
          }
        },
      });
    },
  },
});
